//
//  shiPinTableViewCell.m
//  ProJectB_1
//
//  Created by lanou3g on 16/8/15.
//  Copyright © 2016年 yangshukai. All rights reserved.
//

#import "shiPinTableViewCell.h"
#import "Masonry.h"
#import "ShiPinButton.h"
#import "UIImageView+WebCache.h"
#import "Header.h"
#import <AVFoundation/AVFoundation.h>

#import "PlayerManager.h"

#import <ImageIO/ImageIO.h>

@interface shiPinTableViewCell ()

@property (nonatomic,strong) UIImageView *profile_image;
@property (nonatomic,strong) UILabel *name;
@property (nonatomic,strong) UILabel *passtime;


// 自定义button
@property (nonatomic,strong) ShiPinButton *dingButton;
@property (nonatomic,strong) ShiPinButton *caiButton;
@property (nonatomic,strong) ShiPinButton *repostButton;
@property (nonatomic,strong) ShiPinButton *commentButton;




@property (nonatomic,strong) AVPlayer *player;
@property (nonatomic,strong) AVPlayerItem *item;
@property (nonatomic,strong) AVPlayerLayer *playLayer;




@end

@implementation shiPinTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        /// 添加控件
        self.profile_image = [[UIImageView alloc]init];
        _profile_image.layer.cornerRadius = 20;
        _profile_image.layer.masksToBounds = YES;
        [self.contentView addSubview:_profile_image];
        
        self.name = [[UILabel alloc]init];
        
        _name.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:_name];
        
        self.passtime = [[UILabel alloc]init];
        _passtime.font = [UIFont systemFontOfSize:14];
        
        [self.contentView addSubview:_passtime];
        
        self.text = [[UILabel alloc]init];
        _text.font = [UIFont systemFontOfSize:14];
        _text.numberOfLines = 0;
        [self.contentView addSubview:self.text];
        
#warning 图片
        self.image1 = [[UIImageView alloc]init];
        _image1.backgroundColor = [UIColor redColor];
//        _image1.contentMode = UIViewContentModeScaleAspectFit;
        [self.contentView addSubview:_image1];
        
        self.dingButton = [[ShiPinButton alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        [self.contentView addSubview:_dingButton];
        
        self.caiButton = [[ShiPinButton alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        [self.contentView addSubview:_caiButton];
        
        self.repostButton = [[ShiPinButton alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        [self.contentView addSubview:_repostButton];
        
        self.commentButton = [[ShiPinButton alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        [self.contentView addSubview:_commentButton];
        
        self.videoTimeLable = [[UILabel alloc]init];
        _videoTimeLable.alpha = 0.5;
        _videoTimeLable.backgroundColor = [UIColor lightGrayColor];
        [self.contentView addSubview:_videoTimeLable];
        
        self.playTime = [[UILabel alloc]init];
        _playTime.alpha = 0.5;
        _playTime.backgroundColor = [UIColor lightGrayColor];
        [self.contentView addSubview:_playTime];
        
        self.playButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_playButton setBackgroundImage:[UIImage imageNamed:@"play"] forState:(UIControlStateNormal)];
        [self.contentView addSubview:_playButton];
        
        
        
    }
    return self;
}

//- (void)playerView {
//    
//    NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:14]};
//    NSString *text = self.shiPinModelU.text;
//    CGFloat textHeight = [text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 20, 10000000) options:NSStringDrawingUsesLineFragmentOrigin attributes:attribute context:nil].size.height;
//    
//    self.item = [[AVPlayerItem alloc]initWithURL:[NSURL URLWithString:self.shiPinModelU.videouri]];
//    self.player = [AVPlayer playerWithPlayerItem:_item];
//    self.playLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
//    _playLayer.videoGravity = AVLayerVideoGravityResizeAspect;
//    [self.layer addSublayer:_playLayer];
//}


// Masonry约束
- (void)layoutSubviews {
    __weak typeof(self)weakSelf = self;

    
    CGSize imageSize = [self getImageSizeWithURL:[NSURL URLWithString:self.shiPinModelU.image1]];
//    sleep(4);
    NSLog(@"imageSize=================%f, %f", imageSize.width, imageSize.height);
    [_image1 sd_setImageWithURL:[NSURL URLWithString:self.shiPinModelU.image1]];
#pragma mark ---profile
    [self.profile_image mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(4);
        make.left.offset(8);
        make.width.offset(40);
        make.height.offset(40);
    }];
    
#pragma mark --name
    [self.name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.profile_image.mas_right).offset(4);
        make.top.equalTo(weakSelf.profile_image.mas_top).offset(0);
        make.height.offset(20);

    }];
#pragma mark ---passTime
    [self.passtime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.profile_image.mas_right).offset(4);
        make.top.equalTo(weakSelf.profile_image.mas_top).offset(20);
        make.height.offset(20);

    }];
    
#pragma mark ---text
//    [self.text mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(weakSelf.profile_image.mas_bottom).offset(10);
//        make.left.offset(10);
//        make.right.offset(-10);
    
    self.text.frame = CGRectMake(10, 0, [UIScreen mainScreen].bounds.size.width - 20, 20);
    NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:14]};
    
    
    NSString *text = weakSelf.shiPinModelU.text;
    
    CGSize retSize = [text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 20, 10000000) options:NSStringDrawingUsesLineFragmentOrigin attributes:attribute context:nil].size;
    
    CGFloat yyy = retSize.height;
    CGRect rect = weakSelf.text.frame;
    rect.origin.x = 10;
    rect.origin.y = 50;
    rect.size.height = yyy;
    
    weakSelf.text.frame = rect;
    
#pragma mark ----image1
    [self.image1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.text.mas_bottom).offset(10);
        make.centerX.equalTo(weakSelf.mas_centerX);
        if (((imageSize.width > [UIScreen mainScreen].bounds.size.width - 20) || (imageSize.height > ((weakSelf.bounds.size.width - 20) / 5 * 3)))) {
            if (imageSize.width / imageSize.height > ([UIScreen mainScreen].bounds.size.width - 20) / ((weakSelf.bounds.size.width - 20) / 5 * 3)) {
                // 比较宽
                make.width.offset([UIScreen mainScreen].bounds.size.width - 20);
                make.height.offset(imageSize.height * ([UIScreen mainScreen].bounds.size.width - 20) / imageSize.width);
            } else {
                // 比较高
                make.height.offset(((weakSelf.bounds.size.width - 20) / 5 * 3));
                make.width.offset(imageSize.width * ((weakSelf.bounds.size.width - 20) / 5 * 3) / imageSize.height);
            }
        } else if (imageSize.width > [UIScreen mainScreen].bounds.size.width - 20) {
            make.width.offset([UIScreen mainScreen].bounds.size.width - 20);
            make.height.offset(([UIScreen mainScreen].bounds.size.width - 20)*(imageSize.height)/(imageSize.width));

        } else if (imageSize.height > ((weakSelf.bounds.size.width - 20) / 5 * 3)) {
            make.height.offset(((weakSelf.bounds.size.width - 20) / 5 * 3));
            make.width.offset((imageSize.width)*((weakSelf.bounds.size.width - 20) / 5 * 3)/(imageSize.height));
        }
    }];
    
    [self.dingButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.image1.mas_bottom).offset(10);
        make.left.offset(0);
//        make.bottom.offset(0);
        make.height.offset(selfSizeWidth * 10);
        make.width.offset(selfSizeWidth * 25);
    }];
    
    [self.caiButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.image1.mas_bottom).offset(10);
        make.left.equalTo(weakSelf.dingButton.mas_right).offset(0);
//        make.bottom.offset(0);
        make.height.offset(selfSizeWidth * 10);
        make.width.offset(selfSizeWidth * 25);
    }];
    
    [self.repostButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.image1.mas_bottom).offset(10);
        make.left.equalTo(weakSelf.caiButton.mas_right).offset   (0);
//        make.bottom.offset(0);
        make.height.offset(selfSizeWidth * 10);
        make.width.offset(selfSizeWidth * 25);
    }];
    
    [self.commentButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.image1.mas_bottom).offset(10);
        make.left.equalTo(weakSelf.repostButton.mas_right).offset(0);
//        make.bottom.offset(0);
        make.height.offset(selfSizeWidth * 10);
        make.width.offset(selfSizeWidth * 25);
    }];
    
    
    [_playTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.bottom.equalTo(_dingButton.mas_top).offset(-10);
        make.height.offset(selfSizeHeight * 9);
//        make.width.offset(selfSizeWidth * 15);
    }];
    
    [_videoTimeLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-10);
        make.bottom.equalTo(_dingButton.mas_top).offset(-10);
        make.height.offset(selfSizeHeight * 9);
//        make.width.offset(selfSizeWidth * 15);
    }];
    
    
    [_playButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(_image1.mas_centerX);
        make.center.equalTo(_image1.mas_centerY);
    }];
    
    
#pragma mark ---赋值
     [_profile_image sd_setImageWithURL:[NSURL URLWithString:self.shiPinModelU.profile_image]];
    
    _name.text = self.shiPinModelU.name;
    _passtime.text = self.shiPinModelU.passtime;
    
     _text.text = self.shiPinModelU.text;
    
    
    _dingButton.numberLabel.text = self.shiPinModelU.ding;
    _dingButton.imageName = @"zan";
    _caiButton.numberLabel.text = self.shiPinModelU.cai;
    _caiButton.imageName = @"cai";
    _repostButton.numberLabel.text = self.shiPinModelU.repost;
    _repostButton.imageName = @"pingLun";
    _commentButton.numberLabel.text = self.shiPinModelU.comment;
    _commentButton.imageName = @"comment";
    
    NSInteger time = [self.shiPinModelU.videotime integerValue];
    
    _videoTimeLable.text = [NSString stringWithFormat:@"%.2ld:%.2ld  ", time / 60,time % 60];
    
    _playTime.text = [NSString stringWithFormat:@"  播放%ld次", [self.shiPinModelU.playcount integerValue]];
    
}












// 根据图片url获取图片尺寸
- (CGSize)getImageSizeWithURL:(id)imageURL
{
    NSURL* URL = nil;
    if([imageURL isKindOfClass:[NSURL class]]){
        URL = imageURL;
    }
    if([imageURL isKindOfClass:[NSString class]]){
        URL = [NSURL URLWithString:imageURL];
    }
    if(URL == nil)
        return CGSizeZero;                  // url不正确返回CGSizeZero
    
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:URL];
    NSString* pathExtendsion = [URL.pathExtension lowercaseString];
    
    CGSize size = CGSizeZero;
    if([pathExtendsion isEqualToString:@"png"]){
        size =  [self getPNGImageSizeWithRequest:request];
    }
    else if([pathExtendsion isEqual:@"gif"])
    {
        size =  [self getGIFImageSizeWithRequest:request];
    }
    else{
        size = [self getJPGImageSizeWithRequest:request];
    }
    if(CGSizeEqualToSize(CGSizeZero, size))                    // 如果获取文件头信息失败,发送异步请求请求原图
    {
        NSData* data = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:URL] returningResponse:nil error:nil];
        UIImage* image = [UIImage imageWithData:data];
        if(image)
        {
            size = image.size;
        }
    }
    return size;
}
//  获取PNG图片的大小
 - (CGSize)getPNGImageSizeWithRequest:(NSMutableURLRequest*)request
{
    [request setValue:@"bytes=16-23" forHTTPHeaderField:@"Range"];
    NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    if(data.length == 8)
    {
        int w1 = 0, w2 = 0, w3 = 0, w4 = 0;
        [data getBytes:&w1 range:NSMakeRange(0, 1)];
        [data getBytes:&w2 range:NSMakeRange(1, 1)];
        [data getBytes:&w3 range:NSMakeRange(2, 1)];
        [data getBytes:&w4 range:NSMakeRange(3, 1)];
        int w = (w1 << 24) + (w2 << 16) + (w3 << 8) + w4;
        int h1 = 0, h2 = 0, h3 = 0, h4 = 0;
        [data getBytes:&h1 range:NSMakeRange(4, 1)];
        [data getBytes:&h2 range:NSMakeRange(5, 1)];
        [data getBytes:&h3 range:NSMakeRange(6, 1)];
        [data getBytes:&h4 range:NSMakeRange(7, 1)];
        int h = (h1 << 24) + (h2 << 16) + (h3 << 8) + h4;
        return CGSizeMake(w, h);
    }
    return CGSizeZero;
}
//  获取gif图片的大小
 - (CGSize)getGIFImageSizeWithRequest:(NSMutableURLRequest*)request
{
    [request setValue:@"bytes=6-9" forHTTPHeaderField:@"Range"];
    NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    if(data.length == 4)
    {
        short w1 = 0, w2 = 0;
        [data getBytes:&w1 range:NSMakeRange(0, 1)];
        [data getBytes:&w2 range:NSMakeRange(1, 1)];
        short w = w1 + (w2 << 8);
        short h1 = 0, h2 = 0;
        [data getBytes:&h1 range:NSMakeRange(2, 1)];
        [data getBytes:&h2 range:NSMakeRange(3, 1)];
        short h = h1 + (h2 << 8);
        return CGSizeMake(w, h);
    }
    return CGSizeZero;
}
//  获取jpg图片的大小
- (CGSize)getJPGImageSizeWithRequest:(NSMutableURLRequest*)request
{
    [request setValue:@"bytes=0-209" forHTTPHeaderField:@"Range"];
    NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    
    if ([data length] <= 0x58) {
        return CGSizeZero;
    }
    
    if ([data length] < 210) {// 肯定只有一个DQT字段
        short w1 = 0, w2 = 0;
        [data getBytes:&w1 range:NSMakeRange(0x60, 0x1)];
        [data getBytes:&w2 range:NSMakeRange(0x61, 0x1)];
        short w = (w1 << 8) + w2;
        short h1 = 0, h2 = 0;
        [data getBytes:&h1 range:NSMakeRange(0x5e, 0x1)];
        [data getBytes:&h2 range:NSMakeRange(0x5f, 0x1)];
        short h = (h1 << 8) + h2;
        return CGSizeMake(w, h);
    } else {
        short word = 0x0;
        [data getBytes:&word range:NSMakeRange(0x15, 0x1)];
        if (word == 0xdb) {
            [data getBytes:&word range:NSMakeRange(0x5a, 0x1)];
            if (word == 0xdb) {// 两个DQT字段
                short w1 = 0, w2 = 0;
                [data getBytes:&w1 range:NSMakeRange(0xa5, 0x1)];
                [data getBytes:&w2 range:NSMakeRange(0xa6, 0x1)];
                short w = (w1 << 8) + w2;
                short h1 = 0, h2 = 0;
                [data getBytes:&h1 range:NSMakeRange(0xa3, 0x1)];
                [data getBytes:&h2 range:NSMakeRange(0xa4, 0x1)];
                short h = (h1 << 8) + h2;
                return CGSizeMake(w, h);
            } else {// 一个DQT字段
                short w1 = 0, w2 = 0;
                [data getBytes:&w1 range:NSMakeRange(0x60, 0x1)];
                [data getBytes:&w2 range:NSMakeRange(0x61, 0x1)];
                short w = (w1 << 8) + w2;
                short h1 = 0, h2 = 0;
                [data getBytes:&h1 range:NSMakeRange(0x5e, 0x1)];
                [data getBytes:&h2 range:NSMakeRange(0x5f, 0x1)];
                short h = (h1 << 8) + h2;
                return CGSizeMake(w, h);
            }
        } else {
            return CGSizeZero;
        }
    }
}
























@end
